package com.commerceerp.system.controller;


import com.commerceerp.system.entity.Constant;
import com.commerceerp.system.service.OFRecordService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.RdPage;
import com.commerceerp.system.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "数据接口", value = "数据接口")
@Scope("prototype")
@RestController
@EnableAutoConfiguration
public class OFRecordController {

    @Autowired
    private OFRecordService ofRecordService;


    @ApiOperation(value = "获取已完成订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllOFRecord", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllOFRecord(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                      @RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime,
                                      @RequestParam(value = "current") Integer current,
                                      @RequestParam(value = "pageSize") Integer pageSize) throws IOException {


        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));

        List<Map> list = ofRecordService.getAllOFRecord(goodSystemSku,beginTime,endTime);

        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,rdPage);
    }


    @ApiOperation(value = "获取已完成订单折线图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")
    })
    @RequestMapping(value = "/getProfitDiscount", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getProfitDiscount(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                      @RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime) throws IOException {



        Map<String,Object> map= ofRecordService.getProfitDiscount(goodSystemSku,beginTime,endTime);

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",map,null);
    }


    @ApiOperation(value = "SKU销量排行榜")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllFORecordSkuRate", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllFORecordSkuRate(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                           @RequestParam(value = "beginTime", required = false) String beginTime,
                                           @RequestParam(value = "endTime", required = false) String endTime,
                                           @RequestParam(value = "current") Integer current,
                                           @RequestParam(value = "pageSize") Integer pageSize) throws IOException {

        if(current == null){
            current = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));
        List<Map> list= ofRecordService.getAllFORecordSkuRate(goodSystemSku,beginTime,endTime);
        int r = (current - 1) * pageSize;
        for(Map map : list){
            r++;
            map.put("rownum",r);
        }
        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));
        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,rdPage);
    }


    @ApiOperation(value = "获取SKU成交时间分布图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")
    })
    @RequestMapping(value = "/getSkuOFNumber", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getSkuOFNumber(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                           @RequestParam(value = "beginTime", required = false) String beginTime,
                                           @RequestParam(value = "endTime", required = false) String endTime) throws IOException {


        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",ofRecordService.getSkuOFNumber(goodSystemSku,beginTime,endTime),null);
    }


    @ApiOperation(value = "获取SKU成交时间分布图-汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")
    })
    @RequestMapping(value = "/getOFRGoodPriceCollect", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOFRGoodPriceCollect(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                    @RequestParam(value = "beginTime", required = false) String beginTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) throws IOException {

        List<Map> list = ofRecordService.getOFRGoodPriceCollect(goodSystemSku,beginTime,endTime);

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,null);
    }

    @ApiOperation(value = "获取SKU成交时间分布图-明细")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "goodPrice", value = "商品价格", required = true, dataType = "String")
    })
    @RequestMapping(value = "/getOFRGoodPriceDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOFRGoodPriceDetail(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                            @RequestParam(value = "beginTime", required = false) String beginTime,
                                            @RequestParam(value = "endTime", required = false) String endTime,
                                            @RequestParam(value = "goodPrice") String goodPrice) throws IOException {

        List<Map> list = ofRecordService.getOFRGoodPriceDetail(goodSystemSku,beginTime,endTime,goodPrice);

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,null);
    }


    @ApiOperation(value = "成交价占比图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/getOFRPriceRate", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOFRPriceRate(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                           @RequestParam(value = "beginTime", required = false) String beginTime,
                                           @RequestParam(value = "endTime", required = false) String endTime) throws IOException {

        Map<String,Object> map = ofRecordService.getOFRPriceRate(goodSystemSku,beginTime,endTime);

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",map,null);
    }

    @ApiOperation(value = "用户分布排行榜")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")

    })
    @RequestMapping(value = "/getUserDistribution", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getUserDistribution(String goodSystemSku,String beginTime, String endTime,Integer current,Integer pageSize) throws IOException {
        if(current == null){
            current = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));
        List<Map> list= ofRecordService.getUserDistribution(goodSystemSku,beginTime,endTime);
        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));
        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",list,rdPage);

    }


    @ApiOperation(value = "客户分布占比图")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "goodSystemSku", value = "商品sku", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/getUserDistributionRate", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getUserDistributionRate(@RequestParam(value = "goodSystemSku", required = false) String goodSystemSku,
                                     @RequestParam(value = "beginTime", required = false) String beginTime,
                                     @RequestParam(value = "endTime", required = false) String endTime) throws IOException {

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",ofRecordService.getUserDistributionRate(goodSystemSku,beginTime,endTime),null);
    }


    @ApiOperation(value = "获取下拉框商品sku")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/getAllSystemGoodSKu", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllSystemGoodSKu() throws IOException {

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",ofRecordService.getAllSystemGoodSKu(),null);
    }



    @ApiOperation(value = "获取最近6天成交，未成交订单金额")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/getOrderMoneyDiscount", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOrderMoneyDiscount() throws IOException {

        Map<String,Object> map= ofRecordService.getOrderMoneyDiscount();

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",map,null);
    }


    @ApiOperation(value = "获取最近6天获取成交，未成交订单数量")
    @ApiImplicitParams({

    })
    @RequestMapping(value = "/getOrderNumDiscount", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOrderNumDiscount() throws IOException {

        Map<String,Object> map= ofRecordService.getOrderNumDiscount();

        return  new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",map,null);
    }



}
